package com.qdxwx.manage.controller.coupon;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.coupon.CouponCodeService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.coupon.CouponCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 优惠卷种类信息Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/coupon/code")
public class CouponCodeController extends BaseController {


    @Autowired
    private CouponCodeService couponCodeService;



    //优惠券详情页
    @GetMapping("/list.html")
    public String CouponInfo(HttpServletRequest request, ModelMap mmap) {
        long couponId = TKConvert.toLong(request.getParameter("id"));
        if(couponId < 1) return error500;
        mmap.put("couponId", couponId);
        return  "coupon/coupon-code-list";
    }

    /**
     * 查询优惠券详情列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo CouponCodeList(HttpServletRequest request) {
        CouponCode couponCode = new CouponCode();
        couponCode.setUserName(request.getParameter("userName"));
        couponCode.setCouponCode(TKString.queryKey(request.getParameter("couponCode")));
        couponCode.setStatus(TKConvert.toInteger(request.getParameter("status")));
        couponCode.setCouponId(TKConvert.toLong(request.getParameter("couponId")));
        List<CouponCode> list = couponCodeService.getList(couponCode, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(couponCodeService.count(couponCode));
        return rspData;
    }

    /**
     * 优惠券作废或恢复
     */
    @PostMapping("/status.json")
    @ResponseBody
    public AjaxResult status(HttpServletRequest request) {
        long id = TKConvert.toLong(request.getParameter("id"));
        int status = TKConvert.toInteger(request.getParameter("status"));
        if(id < 1 || status < 1) return error("参数异常");
        CouponCode couponCode = new CouponCode();
        couponCode.setId(id);
        couponCode.setStatus(status);
        return toAjax(couponCodeService.update(couponCode));
    }

}
